<?php

namespace Hilaoyu\LaravelExtend\Services\AccessControl;


use Hilaoyu\LaravelExtend\Services\Auth\Traits\ModelWithLoginRefreshTrait;
use Hilaoyu\Utils\UtilSupport;
use Hilaoyu\LaravelExtend\Model;

abstract class AbstractModelAccessControlEntities extends Model
{

    protected $table = 'access_control_entities';
    protected $keyType = 'char';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'id' ,
        'name' ,
        'type' ,
    ];

    protected $appends = [
      'type_name'
    ];

    static $_allow_types = [
        'resource' => '资源',
        'boolean' => '逻辑',
        'limit' => '限制',
    ];


    static public function GetAllowTypes($key='',$default=null){
        $key = trim($key);
        if($key){
            return data_get(static::$_allow_types,$key,$default);
        }
        return static::$_allow_types;
    }


    static $_allow_actions = [
        'resource' =>[
            'create' => '创建',
            'view_all' => '查看全部',
            //'view_group' => '查看同组用户的',
            'view_own' => '查看自己的',
            'edit_all' => '编辑全部',
            //'edit_group' => '编辑同组用户的',
            'edit_own' => '编辑自己的',
            'delete_all' => '删除全部',
            //'delete_group' => '删除同组用户的',
            'delete_own' => '删除自己的',
        ],
        'boolean' =>[
            'allow' => '允许',
        ],
        'limit' =>[
            'limit' => '限制', // -1为不限制
        ],

    ];


    static public function GetAllowActions($key='',$default=null){
        $key = trim($key);
        if($key){
            return data_get(static::$_allow_actions,$key,$default);
        }
        return static::$_allow_actions;
    }

    public function getValidationRules()
    {
        $rules = [
            'id' => 'required|string|max:36|unique:' . static::getTableCon() . ',id' ,
            'type' => 'required|in:'.implode(',',array_keys(static::GetAllowTypes())),
        ];

        return $rules;
    }


    public function getTypeNameAttribute()
    {
        if($type = $this->getAttribute('type')){
            return static::GetAllowTypes($type,'');
        }
        return '';
    }


}
